# coding=utf-8
# author='kungege21'
# date=2023/8/7 14:26
import uvicorn
from fastapi import FastAPI, Depends
from fastapi.exceptions import RequestValidationError
from starlette.exceptions import HTTPException
from starlette.responses import PlainTextResponse
from starlette.staticfiles import StaticFiles

from turorial import app03, app04, app05, app06, app07, app08


# 全局依赖
async def main_depends():
    print('main_depends')

app = FastAPI(title='FastAPI Kungege',
              description='FastAPI教学练习',
              version='1.0.0',
              docs_url='/docs',
              redoc_url='/redoc',
              dependencies=[Depends(main_depends)])

# app挂载静态文件，mount表示将某个目录下一个完全独立的应用挂载过来，不会在API交互文档中显示
# http://127.0.0.1:8000/static/33.jpg
app.mount(path='/static', app=StaticFiles(directory='./coronavirus/static'))

app.include_router(app03, prefix='/chapter03', tags=['第三章 请求参数和验证'])
app.include_router(app04, prefix='/chapter04', tags=['第四章 响应处理和FastAPI配置'])
app.include_router(app05, prefix='/chapter05', tags=['第五章 FastAPI的依赖注入系统'])
app.include_router(app06, prefix='/chapter06', tags=['第六章 安全、认证和授权'])
app.include_router(app07, prefix='/chapter07', tags=['第七章 FastAPI的数据库操作和多应用的目录结构设计'])
app.include_router(app08, prefix='/chapter08', tags=['第八章 中间件、CORS、后台任务、测试用例'])


# @app.exception_handler(HTTPException)   # 重写HttpException异常处理
# async def http_exception_handler(request, exc):
#     """
#     :param request: 不能省的参数
#     :param exc:
#     :return:
#     """
#     return PlainTextResponse(str(exc.detail), status_code=exc.status_code)
#
#
# @app.exception_handler(RequestValidationError)   # 重写请求验证异常处理器
# async def http_exception_handler(request, exc):
#     """
#     :param request: 不能省的参数
#     :param exc:
#     :return:
#     """
#     return PlainTextResponse(str(exc), status_code=400)


if __name__ == '__main__':
    uvicorn.run('run:app',
                host='0.0.0.0',
                port=8000,
                reload=True,
                )
